﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataLayer.Infrastructure;
using ModelLayer;

namespace BusinessLayer
{
    public class UserManager
    {

        public IEnumerable<User> GetAllUsers()
        {
            using (var uow = new UnitOfWork())
            {
                return uow.UserRepo.Get(includeProperties: x => x.UserAddresses);
            }
        }

        public User GetById(long userId)
        {
            using (var uow = new UnitOfWork())
            {
                return uow.UserRepo.Get(x => x.UserId == userId, null, x => x.UserAddresses).FirstOrDefault();
            }
        }

        public User GetByAspNetId(string userId)
        {
            using (var uow = new UnitOfWork())
            {
                return uow.UserRepo.Get(x => x.AspNetUser.Id == userId, null, x => x.UserCarts.Select(uc => uc.Product)).FirstOrDefault();
            }
        }

        public bool AddUpdate(User user)
        {
            using (var uow = new UnitOfWork())
            {
                if (user.UserId > 0)
                {
                    uow.UserRepo.Update(user);
                }
                else
                {
                    uow.UserRepo.Insert(user);
                }

                return uow.SaveChanges() > 0;
            }
        }

        public bool UpdateProfile(User userDc)
        {
            using (var uow = new UnitOfWork())
            {
                var userDm = uow.UserRepo.Get(x => x.UserId == userDc.UserId, null, x => x.UserAddresses).FirstOrDefault();
                userDm.FirstName = userDc.FirstName;
                userDm.LastName = userDc.LastName;
                userDm.MobileNo = userDc.MobileNo;
                var addressDc = userDc.UserAddresses.FirstOrDefault();
                if (userDm.UserAddresses != null)
                {
                    //implement this logic using userAddressId. Managing multiple addresses.
                    if (userDm.UserAddresses.Count > 0)
                    {
                        var address = userDm.UserAddresses.FirstOrDefault();

                        address.PrimaryAddress = addressDc.PrimaryAddress;
                        address.SecondaryAddress = addressDc.SecondaryAddress;
                        address.City = addressDc.City;
                        address.State = addressDc.State;
                        address.Country = addressDc.Country;
                        address.ZipCode = addressDc.ZipCode;
                    }
                    else
                    {
                        userDm.UserAddresses = new List<UserAddress>()
                        {
                            new UserAddress()
                            {
                                PrimaryAddress   = addressDc.PrimaryAddress  ,
                                SecondaryAddress = addressDc.SecondaryAddress,
                                City             = addressDc.City            ,
                                State            = addressDc.State           ,
                                Country          = addressDc.Country         ,
                                ZipCode          = addressDc.ZipCode
                            }
                        };
                    }
                }
                else
                {
                    userDm.UserAddresses = new List<UserAddress>()
                    {
                        new UserAddress()
                        {
                            PrimaryAddress   = addressDc.PrimaryAddress  ,
                            SecondaryAddress = addressDc.SecondaryAddress,
                            City             = addressDc.City            ,
                            State            = addressDc.State           ,
                            Country          = addressDc.Country         ,
                            ZipCode          = addressDc.ZipCode
                        }
                    };
                }
                uow.UserRepo.Update(userDm);
                return uow.SaveChanges() > 0;
            }
        }

        public bool Delete(long userId)
        {
            using (var uow = new UnitOfWork())
            {
                var user = uow.UserRepo.GetById(userId);
                uow.UserRepo.Delete(user);

                return uow.SaveChanges() > 0;
            }
        }

        public string GetRole(long userId)
        {
            using (var uow = new UnitOfWork())
            {
                var user = uow.UserRepo.Get(x => x.UserId == userId, null, x => x.AspNetUser.AspNetRoles).FirstOrDefault();
                return user.AspNetUser.AspNetRoles.FirstOrDefault().Name;
            }
        }
    }
}
